package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.CompreDetailsBean
import com.stars.empoly.utils.PopuGuideUtils
import com.stars.empoly.utils.PopuImgUtils
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.DialUtils
import kotlinx.coroutines.launch

class DriverDetailsVModel(application: Application) : BViewModel<BRepository>(application) {

   var image=""
   var order_id:String?= ""
   var bean= MutableLiveData<CompreDetailsBean>()
   var state= MutableLiveData<Int>()//订单状态

   var ll:LinearLayout?=null

   var index:String?="" //刷新消息列表用的

   //描述
   var describe= MutableLiveData<String>()



   fun showTips(payment:Int):String{//0全款,1=到付,2=付定金
      if(payment==0){
         return " (全款) "
      }
      if(payment==1){
         return " (到付) "
      }
      return " (付定金) "
   }

   fun showButTips(state:Int):String{
      if(state==2){
         return "到达发货地点"
      }
      if(state==3){
         return "前往目的地点"
      }
      if(state==4){
         return "到达目的地点"
      }
      if(state==5){
         return "等待货主确认"
      }
      if(state==6){
         return "已完成"
      }
      return ""
   }


   fun isShowChat(state:Int,type:Int):Int{

      if(state==6){
         return View.GONE
      }
      if(type==1||type==2||type==3){
         return View.GONE
      }
      return View.VISIBLE
   }
   fun toCallPhone(mobile: String?) {
      //拨打电话
      if (null!=mobile&& TextUtils.isEmpty(mobile)) {
         toast("没有该货主的电话！！")
         return
      }
      DialUtils.dialPhone(view()?.context(), mobile!!)

   }

   fun toChangeOrder(state:Int){

      if(state==2){//到达出发地

         setOrderStatus("3")
         return
      }
      if(state==3){

         setOrderStatus("4")
         return
      }

      if(state==4){

         setOrderStatus("5")
         return
      }


   }

   var  popular: PopuImgUtils? =null
   var service_pic:String?=null


   fun setOrderStatus(toState:String?){//改变订单状态


      if(toState.equals("5")){

         //todo：上传照片
          if(null==popular) {
             popular = PopuImgUtils
          }
         Log.e("-shy-", "popular=: "+popular )
         popular?.show(repository().context as Activity,ll!!,object :ChoiceData{
            override fun onRestuse(id: String?) {

               viewModelScope.launch {
                  var it = repository().lreq { NetEngine.service.updateOrder(order_id, toState, service_pic) }
                  if (it.ok()) {
                     state.value = toState!!.toInt()

                     describe.value="司机到达目的地"

                  }
               }
               popular=null
            }
         })

      }else {

         viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.updateOrder(order_id, toState, null) }
            if (it.ok()) {
               state.value = toState!!.toInt()
               if(toState.equals(3)){
                 describe.value="司机到达出发地"
               }
               if(toState.equals(4)){
                 describe.value="司机正在赶往目的地"
               }
            }

         }
      }


   }


   fun setButColor(state:Int):Int{
      if(state==5||state==6){
         return Color.parseColor("#999999")
      }
      return Color.parseColor("#FF5036")
   }


   fun getInfo() {
      //订单详情

      viewModelScope.launch {
         var it = repository().preq { NetEngine.service.orderDetail(order_id) }

         if (it.ok()) {
            bean.value=it.data
            state.value=it.data?.status
            describe.value=it.data?.describe
         }

      }

   }


   fun isRead(id:String?){


   }


   fun isMessagePush(){

   }
   //导航
   fun toNavigation(position: String, address: String, lat: String, lng: String) {
      if (null == repository().context) {
         return
      }

      PopuGuideUtils.show(repository().context!!, position, address, lat, lng, ll!!)

   }


}



